package com.seleniti.enpareja.controller.ui.view;

import org.ioc.commons.flowcontrol.actioncontroller.ActionController;
import org.ioc.commons.flowcontrol.eventbus.EventBus;
import org.ioc.commons.flowcontrol.logincontroller.IsLoggedUserInfoConsumer;
import org.ioc.commons.flowcontrol.operationmanager.OperationManager;
import org.ioc.commons.flowcontrol.operationmanager.impl.OperationHandlerFactory;
import org.ioc.commons.utils.FormatterLogger;

import com.seleniti.enpareja.controller.common.domain.LoggedUserInfo;
import com.seleniti.enpareja.controller.control.place.EnParejaPlace;
import com.seleniti.enpareja.controller.model.integration.service.AdminService;
import com.seleniti.enpareja.controller.ui.base.EnParejaViewBasePresenter;
import com.seleniti.enpareja.controller.ui.view.AdminConsoleView.Action;
import com.seleniti.enpareja.controller.ui.view.AdminConsoleView.Event;
import com.seleniti.enpareja.controller.ui.view.AdminConsoleView.Operation;

/**
 * Presenter for the SignUp view
 * 
 * @author Jesús Lunar Pérez
 * 
 */
public class AdminConsoleViewPresenter
		extends
		EnParejaViewBasePresenter<AdminConsoleView, AdminConsoleView.Event, AdminConsoleView.Operation, AdminConsoleView.Action>
		implements AdminConsoleView.Presenter, IsLoggedUserInfoConsumer<LoggedUserInfo> {

	private static final FormatterLogger logger = FormatterLogger.getLogger(AdminConsoleViewPresenter.class);

	private AdminService adminService;

	public AdminConsoleViewPresenter(AdminConsoleView display, EventBus<Event> eventbus,
			OperationManager<Operation> operationManager, ActionController<Action> actionController) {
		super(display, eventbus, operationManager, actionController);
	}

	@Override
	public void onInitDisplay() {
		super.onInitDisplay();
		this.adminService = serviceManager.serviceFactory().adminService();
		logger.entering("onInitDisplay");

		this.loginController.requiresLoggedUserInfoIfDisplayActive(this);

		logger.exiting("onInitDisplay");
	}

	@Override
	public void onActive() {
		super.onActive();

		logger.info("Activating Admin console view");

		shell.goBackButtonVisible().setVisible(true);
		shell.refreshButtonVisibility().setVisible(false);

		refreshData();
	}

	@Override
	protected void refreshData() {
		super.refreshData();

		this.adminService.fixInconsistentValues().performing(Operation.FixingInconsistentValues, operationManager)
				.caller(this);

	}

	protected void bindComponents() {
		super.bindComponents();
		operationManager.bindOperation(Operation.FixingInconsistentValues,
				OperationHandlerFactory.<Operation> forLoaders(display.fixingInconsistentValuesIndicator()));
		operationManager.bindOperationsRunning(OperationHandlerFactory.<Operation> forLoaders(shell.globalLoader()));

	}

	@Override
	public void onLoggedUserInfoNotReadyYet() {
		/*
		 * Nothing to do
		 */
	}

	@Override
	public void onLoggedUserInfoDataReady() {
		checkPermissions();
	}

	private void checkPermissions() {
		LoggedUserInfo lui = this.loginController.getLoggedUserInfo();

		if (lui == null || !lui.isAdmin()) {
			this.placeController.goTo(EnParejaPlace.Main);
		}
	}

	@Override
	public void onLoggedUserInfoDataUpdated(LoggedUserInfo loggedUserInfo) {
		checkPermissions();
	}

	@Override
	public void onRemoveNullsFromLists() {
		this.adminService.removeNoneElementsFromList();
	}

}
